*******************************************************************************
/* 	
	Date Created:	2022-05-04
	Author:			Zeynep Somer-Topcu
	Purpose:		Generating the observational data and analyzing Study 2 models
*/
********************************************************************************

clear all
macro drop _all
set more off

* paths
cap cd "~/Dropbox/shared_folders/Margit_Zeynep/distortion/replication, origins"

***********************************************************************************************************************************************************
* STEP 1: creating individual level data to merge with campaign data-- using the CSES survey data or the individual country national election survey data 
***********************************************************************************************************************************************************

* CSES 4

use cses4_reducedcountries.dta, clear

ren D1006_NAM country

replace country="CZ" if country=="Czech Republic"
replace country="DE" if country=="Germany"
replace country="SV" if country=="Sweden"

// Party ID vars
ren (D3018_3 D3018_4) (partyid_which partyid_degree)

// Party&Self perceptions
ren (D3013_A-D3013_I D3014) (position_perc_A position_perc_B position_perc_C position_perc_D ///
						position_perc_E position_perc_F position_perc_G position_perc_H ///
						position_perc_I position_perc_Self)

// Vote Choice:
ren (D3006_LH_PL D3006_LH_DC) (votechoice_list votechoice_distr)

// Political knowledge
gen temp_sum_correct=0
replace temp_sum_correct=1 if D3025_1_A==1
replace temp_sum_correct=temp_sum_correct+1 if D3025_2_A==1
replace temp_sum_correct=temp_sum_correct+1 if D3025_3_A==1
replace temp_sum_correct=temp_sum_correct+1 if D3025_4_A==1

gen pol_knowl_share=temp_sum_correct/4
recode D3025_1_A-D3025_4_A (7 =.)
replace pol_knowl_share=. if (D3025_1_A==. & D3025_2_A==. & D3025_3_A==. & D3025_4_A==.)


// Demographics:
ren D2002 gender

* Age: day and month not asked in SV:
* Calculation by years.
gen age=.
replace age=2014-D2001_Y if country=="SV"
replace age=2013-D2001_Y if country=="DE"
replace age=2013-D2001_Y if country=="CZ"
replace age=. if D2001_Y==9999

ren D2003 education

gen education_new=.
label define educlevels 0 "none" 1 "primary" 2 "secondary" 3 "post sec. or incompl tert" ///
						4 "tertiary"

replace education_new=0 if education==1 
replace education_new=1 if education==2
replace education_new=2 if education==3 | education==4 
replace education_new=3 if education==5
replace education_new=4 if education==6 | education==7 | education==8

label values education_new educlevels

ren D2020 income

ren D2010 employment_status

drop D* temp_*

gen year=.
replace year=2013 if country=="DE"
replace year=2013 if country=="CZ"
replace year=2014 if country=="SV"

gen source="CSES4"

save temp_cses4_micro.dta, replace


//  2. CSES 3- Reduced countries:

use cses3_reducedcountries.dta, clear
		
ren C1006_NAM country

replace country="CZ" if country=="Czech Republic"
replace country="DE" if country=="Germany"
replace country="DK" if country=="Denmark"
replace country="NL" if country=="Netherlands"
replace country="PL" if country=="Poland"
replace country="PO" if country=="Portugal"
replace country="ES" if country=="Spain"

// Party ID vars
ren (C3020_3 C3020_4) (partyid_which partyid_degree)

// Party&Self perceptions
ren (C3011_A-C3011_I C3013) (position_perc_A position_perc_B position_perc_C position_perc_D ///
						position_perc_E position_perc_F position_perc_G position_perc_H ///
						position_perc_I position_perc_Self)

// Vote Choice:
ren (C3023_LH_PL C3023_LH_DC) (votechoice_list votechoice_distr)

// Political knowledge
gen temp_sum_correct=0
replace temp_sum_correct=1 if C3036_1==1
replace temp_sum_correct=temp_sum_correct+1 if C3036_2==1
replace temp_sum_correct=temp_sum_correct+1 if C3036_3==1


gen pol_knowl_share=temp_sum_correct/3
recode C3036_1-C3036_3 (7 9 =.)
replace pol_knowl_share=. if (C3036_1==. & C3036_2==. & C3036_3==.)

// Demographics:
ren C2002 gender

ren C2001 age
replace age=. if age>106

ren C2003 education

gen education_new=.
label define educlevels 0 "none" 1 "primary" 2 "secondary" 3 "post sec. or incompl tert" ///
						4 "tertiary"

replace education_new=0 if education==1 | education==2
replace education_new=1 if education==3 
replace education_new=2 if education==5 | education==4
replace education_new=3 if education==6 | education==7
replace education_new=4 if education==8

label values education_new educlevels

ren C2020 income

ren C2010 employment_status

ren C1018 year

drop C* temp_*

gen source="CSES3"

save temp_cses3_micro.dta, replace


//////////////////////////////////////////////////////////////////////////////////////////////////////////
// 3. UK 2005 from CSESw2:

use cses2_uk.dta, clear

gen country="UK"

// Party ID vars
gen partyid_which=B3029_1 if B3028==1
replace partyid_which=B3035 if B3034==1 & B3028==2
replace  partyid_which=. if  partyid_which==99

ren B3036 partyid_degree


// Party&Self perceptions
ren (B3038_A-B3038_I B3045) (position_perc_A position_perc_B position_perc_C position_perc_D ///
						position_perc_E position_perc_F position_perc_G position_perc_H ///
						position_perc_I position_perc_Self)

// Vote Choice:
ren (B3006_1) (votechoice_distr)
gen votechoice_list=.

// Political knowledge
gen temp_sum_correct=0
replace temp_sum_correct=temp_sum_correct+1 if B3047_1==1
replace temp_sum_correct=temp_sum_correct+1 if B3047_2==1
replace temp_sum_correct=temp_sum_correct+1 if B3047_3==1
replace temp_sum=. if temp_sum==0 & /// 
	(B3047_1==9 & B3047_2==9 & B3047_3==9)

gen pol_knowl_share=temp_sum_correct/3

replace pol_knowl_share=. if (B3047_1==9 & B3047_2==9 & B3047_3==9)

// Demographics:
ren B2002 gender

ren B2001 age

ren B2003 education

gen education_new=.
label define educlevels 0 "none" 1 "primary" 2 "secondary" 3 "post sec. or incompl tert" ///
						4 "tertiary"

replace education_new=0 if education==2 
replace education_new=1 if education==3 | education==4
replace education_new=2 if education==5 
replace education_new=3 if education==6 | education==7
replace education_new=4 if education==8

label values education_new educlevels

ren B2020 income

ren B2010 employment_status

ren B1018 year

drop B* temp_*

gen source="CSESw2"

save temp_uk2005_micro.dta, replace


* 1a. UK 2010 from BNES:

use uk_2010.dta , replace

// Party ID vars
gen partyid_which=p44q1 if p43q1==1
replace partyid_which=p50q1 if missing(partyid_which) & p49q1==1

ren p46q1 partyid_degree
recode partyid_degree (4=8)


// Party&Self perceptions
ren (p31q1 p32q1 p33q1 p34q1 p35q1 p39q1) (position_perc_A position_perc_B position_perc_C ///
										position_perc_D position_perc_E position_perc_self)
/*
A - labour
B - cons
C - libdem
D - SNP
E - plaidcymru
*/

// Vote Choice:
ren (p52q1) (votechoice_distr)
gen votechoice_list=.

// Political knowledge
gen temp_sum_correct=0
replace temp_sum_correct=temp_sum_correct+1 if p83g1==1
replace temp_sum_correct=temp_sum_correct+1 if p83g2==1
replace temp_sum_correct=temp_sum_correct+1 if p83g3==2
replace temp_sum_correct=temp_sum_correct+1 if p83g4==2
replace temp_sum_correct=temp_sum_correct+1 if p83g5==2
replace temp_sum_correct=temp_sum_correct+1 if p83g6==2
replace temp_sum_correct=temp_sum_correct+1 if p83g7==1
replace temp_sum_correct=temp_sum_correct+1 if p83g8==1

gen pol_knowl_share=temp_sum_correct/8

// Demographics:
ren q186 gender

gen temp_DoB=q151+1899
gen age= 2010 - temp_DoB
replace age=. if q151==94

ren q159 education

gen education_new=.
label define educlevels 0 "none" 1 "primary" 2 "secondary" 3 "post sec. or incompl tert" ///
						4 "tertiary"

replace education_new=1 if education>=12 & education<=16
replace education_new=2 if education>=7 & education<=11
replace education_new=3 if education==6 
replace education_new=4 if education<=5 | education==18
replace education_new=0 if education==.

label values education_new educlevels

xtile income=q166, nq(5)

gen year=2010

gen temp_unemployment_B=.
replace temp_unemployment_B=1 if (q167==4|q167==5)
replace temp_unemployment_B=0 if !(q167==4|q167==5|q167==12) 

keep votechoice_* age education_new income year gender age education pol_knowl_share ///
		position_perc_* partyid* temp_unemployment_B

gen source="BNES2010-yougov"
gen country="UK"

save temp_uk2010_micro.dta, replace


* 1b. UK 2015 from BNES:

use uk_2015.dta , replace

// Party ID vars
recode d01 (-2=.) (-1=.) (1=.)
ren d01 partyid_which

recode d03 (-2=.) (-1=.) (1=.)
replace partyid_which=d03 if partyid_which==. & d02==1

ren d04 partyid_degree
recode partyid_degree (-1=.)

// Party&Self perceptions
ren (cses_q1_1-cses_q1_7 e01) ///
	(position_perc_A position_perc_B position_perc_C position_perc_D ///
		position_perc_E position_perc_F position_perc_G position_perc_self)

/*
A - cons
B - labour
C - libdem
D - ukip
E - greens
F - snp
G - plaidcym
*/
				
// Vote Choice:
recode b02 (-2=.) (-1=.)
replace b02=b02+1
ren (b02) (votechoice_distr)
gen votechoice_list=.

// Political knowledge
gen temp_sum_correct=0
replace temp_sum_correct=temp_sum_correct+1 if x01_1==1
replace temp_sum_correct=temp_sum_correct+1 if x01_2==1
replace temp_sum_correct=temp_sum_correct+1 if x01_3==2
replace temp_sum_correct=temp_sum_correct+1 if x01_4==1
replace temp_sum_correct=temp_sum_correct+1 if x01_5==1
replace temp_sum_correct=temp_sum_correct+1 if x01_6==2

gen pol_knowl_share=temp_sum_correct/6


// Demographics:
ren y09 gender

recode Age (-1=.)
ren Age age


tab education
gen education_new=.
label define educlevels 0 "none" 1 "primary" 2 "secondary" 3 "post sec. or incompl tert" ///
						4 "tertiary"

replace education_new=0 if education==0
replace education_new=1 if education>=10 & education<=17
replace education_new=2 if education>=7 & education<=9 | education==5
replace education_new=3 if education==6
replace education_new=4 if (education<=5 & education!=0) | education==18

label values education_new educlevels


// Income
replace y01=. if y01<0
xtile income=y01, nq(5)

gen temp_unemployment_B=.
replace temp_unemployment_B=1 if y02==5 | y02==8
replace temp_unemployment_B=0 if !( y02==5 | y02==8)

gen year=2015

keep votechoice_* age income year gender age education_new pol_knowl_share ///
		position_perc_* partyid* temp_unemployment_B
		
		
gen source="BNES2015-BNES"
gen country="UK"

save temp_uk2015_micro.dta, replace


* 4. PL 2011:

use Poland_2011.dta , replace

// Party ID vars
ren C19b1 partyid_which

ren C19c partyid_degree
recode partyid_degree (7=.) (8=.)

// Party&Self perceptions
ren (C14A-C14E C15) ///
	(position_perc_A position_perc_B position_perc_C position_perc_D ///
		position_perc_E position_perc_self)

/*
A - PiS
B - PO
C - PSL
D - SLD
E - RP
*/

// Vote Choice:
ren (C8b) (votechoice_distr)
gen votechoice_list=.

// Political knowledge
gen temp_sum_correct=0
replace temp_sum_correct=temp_sum_correct+1 if C77a==3
replace temp_sum_correct=temp_sum_correct+1 if C77b==3
replace temp_sum_correct=temp_sum_correct+1 if C77c==1
replace temp_sum_correct=temp_sum_correct+1 if C77d==3

gen pol_knowl_share=temp_sum_correct/4
replace pol_knowl_share=. if (C77a==8&C77b==8&C77c==8&C77d==8)

// Demographics:
ren plec gender

replace dd=1 if dd==.
replace dd=30 if mm==9
gen temp_age=string(dd)+"/"+string(mm)+"/"+string(yyyy)
gen DoB=date(temp_age, "DMY")
format DoB %td

gen age=(td(09oct2011)-DoB)/365


// Education
ren W1 education
gen education_new=.
label define educlevels 0 "none" 1 "primary" 2 "secondary" 3 "post sec. or incompl tert" ///
						4 "tertiary"

replace education_new=1 if education==2 | education==3 | education==5 | ///
							education==7
replace education_new=2 if education==6 | education==8
replace education_new=3 if education==4 | education==9
replace education_new=4 if education>=10 & education<=12
replace education_new=0 if education==1

label values education_new educlevels

// Income
xtile income=M22, nq(5)

gen temp_unemployment_B=1 if W2==14 | W2==16
replace temp_unemployment_B=0 if !(W2==14 | W2==16)

gen year=2011

keep votechoice_* age income year gender age education_new pol_knowl_share ///
		position_perc_* partyid*  temp_unemployment_B

gen source="zbior2011"
gen country="PL"

save temp_pl2011_micro.dta, replace


* 5. Portugal 2011:

use Portugal_2011.dta, replace

// Party ID vars
gen partyid_which=P48
replace partyid_which=P50 if missing(partyid_which) & P49==1
replace partyid_which=. if partyid_which==90 | partyid_which==99

gen partyid_degree=P51
recode partyid_degree (98=.) (3=1) (1=3)


// Party&Self perceptions
ren (P54_1-P54_5) ///
	(position_perc_A position_perc_B position_perc_C position_perc_D ///
		position_perc_E)

/*
A - Bloco Esq
B - CDS-pp
C - CDU
D - PPD-PSD
E - PS
*/

* Self position not asked
gen position_perc_self=.

// Vote Choice:
ren (P33) (votechoice_list)
recode votechoice_list (97 98 99 = .)
gen votechoice_distr=.

// Political knowledge
gen temp_sum_correct=0
replace temp_sum_correct=temp_sum_correct+1 if P62==2
replace temp_sum_correct=temp_sum_correct+1 if P63==3
replace temp_sum_correct=temp_sum_correct+1 if P64==3
replace temp_sum_correct=temp_sum_correct+1 if P65==3

gen pol_knowl_share=temp_sum_correct/4

replace pol_knowl_share=. if (P62==99 & P63==99 & P64==99 & P65==99)

// Demographics:
ren anos age

ren sexo gender

// Education
ren D4 education
gen education_new=.
label define educlevels 0 "none" 1 "primary" 2 "secondary" 3 "post sec. or incompl tert" ///
						4 "tertiary"

replace education_new=1 if education==3 | education==4 
replace education_new=2 if education==5 
replace education_new=3 if education==6 
replace education_new=4 if education==7
replace education_new=0 if education==1 | education==2

label values education_new educlevels

// Income
ren D12 income

gen temp_unemployment_B=1 if D6==5
replace temp_unemployment_B=0 if !(D6==5|D6==99)


gen year=2011

keep votechoice_* age income year gender age education_new pol_knowl_share ///
		position_perc_* partyid*  temp_unemployment_B

gen source="bd2011"
gen country="PO"

save temp_po2011_micro.dta, replace



* 6. NL 2012:

use Netherlands_2012.dta , replace

// Party ID vars (not asked)

gen partyid_which=.
gen partyid_degree=.


// Party&Self perceptions
ren (V120-V129 V130) ///
	(position_perc_A position_perc_B position_perc_C position_perc_D ///
		position_perc_E position_perc_F position_perc_G position_perc_H position_perc_I position_perc_J position_perc_self)

/*
A - CDA
B - PvDA
C - VVD
D - D66
E - GroenLinks
F - SP
G - PPV
H - ChristenUnie
I - SGP
J - PvdD
*/


// Vote Choice:
ren (V212) (votechoice_list)
recode votechoice_list (13 14 = .)
gen votechoice_distr=.

// Political knowledge
gen temp_sum_correct=0
replace temp_sum_correct=temp_sum_correct+1 if V182==2
replace temp_sum_correct=temp_sum_correct+1 if V183==5.8 | V183==6
replace temp_sum_correct=temp_sum_correct+1 if V184==2
replace temp_sum_correct=temp_sum_correct+1 if V185==3

gen pol_knowl_share=temp_sum_correct/4

replace pol_knowl_share=. if (V182==. & V183==. & V184==. &V185==.)

// Demographics:
ren V340 age

ren V341 gender

// Education
ren V344 education
gen education_new=.
label define educlevels 0 "none" 1 "primary" 2 "secondary" 3 "post sec. or incompl tert" ///
						4 "tertiary"

replace education_new=1 if education==1 | education==2 
replace education_new=2 if education==3 
replace education_new=3 if education==4 
replace education_new=4 if education==5

label values education_new educlevels

// Income
ren V359 income
recode income (1 2 = 1) (3 4 =2) (5 6 =3) (7 8 =4) (9 10 =5)

gen temp_unemployment_B=1 if V350==3
replace temp_unemployment_B=0 if !(V350==3|V350==.)

gen year=2012

keep votechoice_* age income year gender age education_new pol_knowl_share ///
		position_perc_* partyid* temp_unemployment_B

gen source="NKO2012"
gen country="NL"

save temp_nl2012_micro.dta, replace



* 7. DK 2011 

use Denmark_2011.dta , replace

// Party ID vars
gen partyid_which=V94
replace partyid_which=V96 if missing(partyid_which)
recode partyid_which (88 .a .b = .)

gen partyid_degree=V95
recode partyid_degree (.a .b 8=. ) (2=3)


// Party&Self perceptions
ren (V240-V247 V239) ///
	(position_perc_A position_perc_B position_perc_C position_perc_D ///
		position_perc_E position_perc_F position_perc_G position_perc_H ///
		position_perc_self)

/*
A - Socialdemo
B - Rad venstre
C - konservative folkep
D - socialistik folkep
E - liberal alliance
F - Dansk Folkep
G - Venstre
H - Enhedslisten
*/


// Vote Choice:
ren (V63) (votechoice_list)
recode votechoice_list (.a = .)
gen votechoice_distr=.

// Political knowledge
gen temp_sum_correct=0
replace temp_sum_correct=temp_sum_correct+1 if V322==4
replace temp_sum_correct=temp_sum_correct+1 if V323==1
replace temp_sum_correct=temp_sum_correct+1 if V324==8
replace temp_sum_correct=temp_sum_correct+1 if V325==7

gen pol_knowl_share=temp_sum_correct/4
replace pol_knowl_share=. if (V322==. & V323==. & V324==. & V325==. )

// Demographics:
ren V367 age

ren V7 gender


// Education
ren V340 education
gen education_new=.
label define educlevels 0 "none" 1 "primary" 2 "secondary" 3 "post sec. or incompl tert" ///
						4 "tertiary"

replace education_new=1 if education==2 | education==3 | education==4
replace education_new=2 if education>=5 & education<=8
replace education_new=3 if education>=5 & education<=8 & (V342>=1 & V342<=6)
replace education_new=4 if education>=5 & education<=8 & (V342>=7 & V342<=10)
replace education_new=0 if education==1

label values education_new educlevels

// Income
recode V326 (88 .a =.)
xtile income=V326, nq(5)

gen temp_unemployment_B=1 if V348>=10 & V348<=12
replace temp_unemployment_B=0 if !((V348>=10 & V348<=12)|V348==88|V348==99)

gen year=2011

keep votechoice_* age income year gender age education_new pol_knowl_share ///
		position_perc_* partyid* temp_unemployment_B

gen source="DK_2011"
gen country="DK"

save temp_dk2011_micro.dta, replace




**********************************************************************************************************************************************************************************
* STEP 2: combining the individual country/election survey data and changing the format to stacked data where each individual enters the data as many times as there are parties 
**********************************************************************************************************************************************************************************

use temp_cses4_micro.dta, clear
append using temp_cses3_micro.dta
append using temp_uk2005_micro.dta
append using temp_uk2010_micro.dta
append using temp_uk2015_micro.dta
append using temp_pl2011_micro.dta
append using temp_po2011_micro.dta
append using temp_nl2012_micro.dta
append using temp_dk2011_micro.dta

_strip_labels _all

sort country year

by country year: gen respondent_id=_n

// Unemployment dummy:
gen unemployed=.
replace unemployed=1 if employment_status==5 & (source=="CSES3"|source=="CSES4"|source=="CSESw2")
replace unemployed=0 if employment_status!=5 & employment_status<97 & (source=="CSES3"|source=="CSES4"|source=="CSESw2")
replace unemployed=temp_unemployment_B if !(source=="CSES3"|source=="CSES4"|source=="CSESw2")

// income
recode income (7/9=.)

// partyid degree
recode partyid_degree (7/9=.)

// labels f¸r education:
label values education_new educlevels						

recode gender (9=.)
recode position_perc_Self (95/99 = .)

ren position_perc_Self self_placement 

gen country_ident=country+string(year)

gen temp_id=_n
reshape long  position_perc_, i(temp_id) j(Party) string

recode position_perc (11/99 -999/-1 = .)



**********************************************************************************************************************************************************************************
* STEP 3: Aligning party numbering between the electoral studies and the CCDP dataset and preparing the data for the merger
**********************************************************************************************************************************************************************************

gen partyid_which2=partyid_which

recode partyid_which2 votechoice_list votechoice_distr (4=6) (6=3) (9=1) (15=5) (17=4) (26=2) (92/99=.) (.=.) (else=888) if  country=="CZ" & year==2010
recode partyid_which2 votechoice_list votechoice_distr (1=1) (2=7) (3=3) (4=5) (5=2) (6=8) (7=4) (92/99=.) (.=.) (else=888) if  country=="CZ" & year==2013

recode partyid_which2 votechoice_list votechoice_distr (1=1) (2=2) (3=4) (4=5) (6=7) (5=6) (92/99=.) (.=.) (else=888) if country=="DE" & year==2009
recode partyid_which2 votechoice_list votechoice_distr (1=3) (2=1) (3=2) (4=4) (5=7) (6=6) (7=5) (8=8) (9=9) (92/99=.) (.=.) (else=888) if country=="DE" & year==2013

recode partyid_which2 votechoice_list votechoice_distr (1=1) (2=3) (3=4) (4=5) (5=6) (6=7) (7=2) (8=8) (9=9) (92/99=.) (.=.)(else=888) if country=="SV" & year==2014

recode partyid_which2 votechoice_list votechoice_distr (1=2) (2=6) (3=5) (4=4) (6=3) (7=1) (8=8) (9=7) (92/99=.) (.=.) (else=888) if country=="DK" & year==2007
recode partyid_which2 votechoice_list votechoice_distr (1=2) (2=6) (3=5) (4=4) (5=9) (7=3) (8=1) (9=7) (11=.) (.=.) (else=888) if country=="DK" & year==2011

recode partyid_which2 votechoice_list votechoice_distr (1=3) (2=1) (3=6) (4=2) (5=5) (6=4) (7=8) (8=7) (92/99=.) (.=.) (else=888) if country=="NL" & year==2010

recode partyid_which2 votechoice_list votechoice_distr (4=1) (6=2) (9=3) (8=4) (92/99=.) (.=.) (else=888) if country=="PL" & year==2007 
recode partyid_which2 (3=2) (7=1) (6=4) (8=7) (4=8) (10=3) (.=.) (else=888) if country=="PL" & year==2011

recode partyid_which2 votechoice_list votechoice_distr (1=4) (2=5) (3=3) (4=2) (5=1) (92/99=.) (.=.) (else=888) if country=="PO" & year==2009
recode partyid_which2 (1=4) (2=5) (3=3) (6=2) (7=1) (90/99=.) (.=.) (else=888) if country=="PO" & year==2011

recode partyid_which2 votechoice_list votechoice_distr (1=1) (2=3) (3=2) (4=5) (94/99=.) (.=.) (else=888) if country=="UK" & year==2005
recode partyid_which2 votechoice_list votechoice_distr (1=1) (2=3) (3=2) (4=5) (7=4) (0=.) (.=.) (else=888) if country=="UK" & year==2010
recode partyid_which2 votechoice_list votechoice_distr (2=1) (4=2) (3=3) (8=4) (5=5) (.=.) (else=888) if country=="UK" & year==2015

recode partyid_which2 votechoice_list votechoice_distr (2=1) (1=2) (3=3) (5=4) (8=5) (6=6) (4=7) (92/99=.) (.=.) (else=888) if country=="ES" & year==2008
recode partyid_which2 (2=1) (1=2) (3=3) (5=4) (8=5) (7=6) (4=7) (97/99=.) (.=.) (else=888) if country=="ES" & year==2011

recode votechoice_list votechoice_distr (1=4) (2=5) (3=3) (4=2) (5=1) (98/99=.) (.=.) (else=888) if country=="PO" & year==2011
recode votechoice_list votechoice_distr (1=2) (2=1) (3=3) (4=8) (5=5) (6=4) (7=7) (9=6) (10=9) (12=10) (.=.)  (995/999=.) (else=888) if country=="NL" & year==2012
recode votechoice_list votechoice_distr (7=2) (1=1) (5=4) (4=7) (3=3) ( 12/98 .=.) (.=.) (else=888) if country=="PL" & year==2011

drop education temp_id employment employment_status

drop partyid_which
rename partyid_which2 partyid_which

// Variable subject-code for the merger:
gen subject=.
replace subject=1 if country=="CZ" & year==2013 & Party=="A"
replace subject=7 if country=="CZ" & year==2013 & Party=="B"
replace subject=3 if country=="CZ" & year==2013 & Party=="C"
replace subject=5 if country=="CZ" & year==2013 & Party=="D"
replace subject=2 if country=="CZ" & year==2013 & Party=="E"
replace subject=8 if country=="CZ" & year==2013 & Party=="F"
replace subject=4 if country=="CZ" & year==2013 & Party=="G"

replace subject=6 if country=="CZ" & year==2010 & Party=="E"
replace subject=3 if country=="CZ" & year==2010 & Party=="D"
replace subject=1 if country=="CZ" & year==2010 & Party=="A"
replace subject=4 if country=="CZ" & year==2010 & Party=="G"
replace subject=2 if country=="CZ" & year==2010 & Party=="B"
replace subject=5 if country=="CZ" & year==2010 & Party=="C"

replace subject=1 if country=="DE" & year==2009 & Party=="A"
replace subject=2 if country=="DE" & year==2009 & Party=="F"
replace subject=4 if country=="DE" & year==2009 & Party=="B"
replace subject=5 if country=="DE" & year==2009 & Party=="C"
replace subject=7 if country=="DE" & year==2009 & Party=="D"
replace subject=6 if country=="DE" & year==2009 & Party=="E"

replace subject=1 if country=="DE" & year==2013 & Party=="A"
replace subject=2 if country=="DE" & year==2013 & Party=="E"
replace subject=4 if country=="DE" & year==2013 & Party=="B"
replace subject=7 if country=="DE" & year==2013 & Party=="C"
replace subject=6 if country=="DE" & year==2013 & Party=="D"
replace subject=5 if country=="DE" & year==2013 & Party=="F"
replace subject=8 if country=="DE" & year==2013 & Party=="G"
replace subject=9 if country=="DE" & year==2013 & Party=="H"

replace subject=1 if country=="SV" & year==2014 & Party=="F"
replace subject=3 if country=="SV" & year==2014 & Party=="A"
replace subject=4 if country=="SV" & year==2014 & Party=="E"
replace subject=5 if country=="SV" & year==2014 & Party=="G"
replace subject=6 if country=="SV" & year==2014 & Party=="B"
replace subject=7 if country=="SV" & year==2014 & Party=="H"
replace subject=2 if country=="SV" & year==2014 & Party=="D"
replace subject=8 if country=="SV" & year==2014 & Party=="C"
replace subject=9 if country=="SV" & year==2014 & Party=="I"

replace subject=2 if country=="DK" & year==2007 & Party=="B"
replace subject=6 if country=="DK" & year==2007 & Party=="F"
replace subject=5 if country=="DK" & year==2007 & Party=="E"
replace subject=4 if country=="DK" & year==2007 & Party=="D"
replace subject=3 if country=="DK" & year==2007 & Party=="C"
replace subject=1 if country=="DK" & year==2007 & Party=="A"
replace subject=8 if country=="DK" & year==2007 & Party=="G"
replace subject=7 if country=="DK" & year==2007 & Party=="H"

replace subject=3 if country=="DK" & year==2011 & Party=="F"
replace subject=7 if country=="DK" & year==2011 & Party=="H"
replace subject=5 if country=="DK" & year==2011 & Party=="C"
replace subject=9 if country=="DK" & year==2011 & Party=="E"
replace subject=6 if country=="DK" & year==2011 & Party=="B"
replace subject=4 if country=="DK" & year==2011 & Party=="D"
replace subject=2 if country=="DK" & year==2011 & Party=="A"
replace subject=1 if country=="DK" & year==2011 & Party=="G"

replace subject=3 if country=="NL" & year==2010 & Party=="A"
replace subject=1 if country=="NL" & year==2010 & Party=="B"
replace subject=6 if country=="NL" & year==2010 & Party=="C"
replace subject=2 if country=="NL" & year==2010 & Party=="D"
replace subject=5 if country=="NL" & year==2010 & Party=="E"
replace subject=4 if country=="NL" & year==2010 & Party=="F"
replace subject=8 if country=="NL" & year==2010 & Party=="G"
replace subject=7 if country=="NL" & year==2010 & Party=="H"

replace subject=2 if country=="NL" & year==2012 & Party=="A"
replace subject=7 if country=="NL" & year==2012 & Party=="H"
replace subject=4 if country=="NL" & year==2012 & Party=="D"
replace subject=8 if country=="NL" & year==2012 & Party=="E"
replace subject=6 if country=="NL" & year==2012 & Party=="G"
replace subject=1 if country=="NL" & year==2012 & Party=="B"
replace subject=9 if country=="NL" & year==2012 & Party=="J"
replace subject=5 if country=="NL" & year==2012 & Party=="F"
replace subject=3 if country=="NL" & year==2012 & Party=="C"

replace subject=1 if country=="PL" & year==2007 & Party=="B"
replace subject=2 if country=="PL" & year==2007 & Party=="A"
replace subject=3 if country=="PL" & year==2007 & Party=="C"
replace subject=4 if country=="PL" & year==2007 & Party=="D"

replace subject=2 if country=="PL" & year==2011 & Party=="B"
replace subject=1 if country=="PL" & year==2011 & Party=="A"
replace subject=7 if country=="PL" & year==2011 & Party=="E"
replace subject=4 if country=="PL" & year==2011 & Party=="C"
replace subject=3 if country=="PL" & year==2011 & Party=="D"

replace subject=4 if country=="PO" & year==2009 & Party=="D"
replace subject=5 if country=="PO" & year==2009 & Party=="C"
replace subject=3 if country=="PO" & year==2009 & Party=="E"
replace subject=2 if country=="PO" & year==2009 & Party=="B"
replace subject=1 if country=="PO" & year==2009 & Party=="A"

replace subject=4 if country=="PO" & year==2011 & Party=="A"
replace subject=5 if country=="PO" & year==2011 & Party=="B"
replace subject=3 if country=="PO" & year==2011 & Party=="C"
replace subject=2 if country=="PO" & year==2011 & Party=="D"
replace subject=1 if country=="PO" & year==2011 & Party=="E"

replace subject=1 if country=="UK" & year==2005 & Party=="A"
replace subject=3 if country=="UK" & year==2005 & Party=="B"
replace subject=2 if country=="UK" & year==2005 & Party=="C"
replace subject=5 if country=="UK" & year==2005 & Party=="D"
 
replace subject=3 if country=="UK" & year==2010 & Party=="B"
replace subject=1 if country=="UK" & year==2010 & Party=="A"
replace subject=2 if country=="UK" & year==2010 & Party=="C"
replace subject=5 if country=="UK" & year==2010 & Party=="D"

replace subject=1 if country=="UK" & year==2015 & Party=="B"
replace subject=2 if country=="UK" & year==2015 & Party=="C"
replace subject=3 if country=="UK" & year==2015 & Party=="A"
replace subject=4 if country=="UK" & year==2015 & Party=="D"
replace subject=5 if country=="UK" & year==2015 & Party=="F"

replace subject=1 if country=="ES" & year==2008 & Party=="A"
replace subject=2 if country=="ES" & year==2008 & Party=="B"
replace subject=3 if country=="ES" & year==2008 & Party=="F"
replace subject=4 if country=="ES" & year==2008 & Party=="C"
replace subject=5 if country=="ES" & year==2008 & Party=="E"
replace subject=6 if country=="ES" & year==2008 & Party=="D"
replace subject=7 if country=="ES" & year==2008 & Party=="I"


drop if missing(subject)

drop if country=="ES" & subject==4 

rename subject subject_cp

gen ccode=11 if country=="SV"
replace ccode=13 if country=="DK"
replace ccode=22 if country=="NL"
replace ccode=33 if country=="ES"
replace ccode=35 if country=="PO"
replace ccode=41 if country=="DE"
replace ccode=51 if country=="UK"
replace ccode=82 if country=="CZ"
replace ccode=92 if country=="PL"

* Handling the separate CDU-CSU and combined CDU-CSU cases
drop if subject_cp==2 & ccode==41 
replace subject_cp=3 if subject_cp==1 & ccode==41


**********************************************************************************************************************************************************************************
* STEP 4:  Reducing the dataset to only the top-two parties in each election and finalizing the data
**********************************************************************************************************************************************************************************

gen top_two=1 if (subject_cp==3 | subject_cp==6)  & ccode==11 & year==2014
replace top_two=1 if (subject_cp==1 | subject_cp==2)  & ccode==13 & year==2007
replace top_two=1 if (subject_cp==1 | subject_cp==2) & ccode==13 & year==2011
replace top_two=1 if (subject_cp==1 | subject_cp==3) & ccode==22 & (year==2010 | year==2012)
replace top_two=1 if (subject_cp==1 | subject_cp==2) & ccode==33 & year==2008
replace top_two=1 if (subject_cp==1 | subject_cp==2) & ccode==35 & (year==2009 | year==2011)
replace top_two=1 if (subject_cp==3 | subject_cp==4) & ccode==41 & (year==2009 | year==2013)
replace top_two=1 if (subject_cp==1 | subject_cp==3) & ccode==51 & (year==2005 | year==2010 | year==2015)
replace top_two=1 if (subject_cp==1 | subject_cp==2) & ccode==82 & year==2010
replace top_two=1 if (subject_cp==1 | subject_cp==7) & ccode==82 & year==2013
replace top_two=1 if (subject_cp==1 | subject_cp==2) & ccode==92 & (year==2007 | year==2011)

drop if top_two!=1

gen other_cp=6 if subject_cp==3 & ccode==11 & year==2014
replace other_cp=3 if subject_cp==6 & ccode==11 & year==2014
replace other_cp=1 if subject_cp==2  & ccode==13 & year==2007
replace other_cp=2 if subject_cp==1  & ccode==13 & year==2007
replace other_cp=1 if subject_cp==2  & ccode==13 & year==2011
replace other_cp=2 if subject_cp==1  & ccode==13 & year==2011
replace other_cp=1 if subject_cp==2  & ccode==33 & year==2008
replace other_cp=2 if subject_cp==1  & ccode==33 & year==2008
replace other_cp=1 if subject_cp==2  & ccode==35 & year==2011
replace other_cp=2 if subject_cp==1  & ccode==35 & year==2011
replace other_cp=1 if subject_cp==2  & ccode==35 & year==2009
replace other_cp=2 if subject_cp==1  & ccode==35 & year==2009
replace other_cp=1 if subject_cp==2  & ccode==82 & year==2010
replace other_cp=2 if subject_cp==1  & ccode==82 & year==2010
replace other_cp=1 if subject_cp==2  & ccode==92 & year==2011
replace other_cp=2 if subject_cp==1  & ccode==92 & year==2011
replace other_cp=1 if subject_cp==2  & ccode==92 & year==2007
replace other_cp=2 if subject_cp==1  & ccode==92 & year==2007
replace other_cp=1 if subject_cp==3  & ccode==22 & year==2010 
replace other_cp=3 if subject_cp==1  & ccode==22 & year==2010 
replace other_cp=1 if subject_cp==3  & ccode==22 & year==2012 
replace other_cp=3 if subject_cp==1  & ccode==22 & year==2012
replace other_cp=1 if subject_cp==3  & ccode==51 & year==2010 
replace other_cp=3 if subject_cp==1  & ccode==51 & year==2010
replace other_cp=1 if subject_cp==3  & ccode==51 & year==2005
replace other_cp=3 if subject_cp==1  & ccode==51 & year==2005
replace other_cp=1 if subject_cp==3  & ccode==51 & year==2015 
replace other_cp=3 if subject_cp==1  & ccode==51 & year==2015
replace other_cp=3 if subject_cp==4 & ccode==41 & year==2009
replace other_cp=4 if subject_cp==3 & ccode==41 & year==2009 
replace other_cp=3 if subject_cp==4 & ccode==41 & year==2013
replace other_cp=4 if subject_cp==3 & ccode==41 & year==2013 
replace other_cp=1 if subject_cp==7 & ccode==82 & year==2013
replace other_cp=7 if subject_cp==1 & ccode==82 & year==2013


gen pid=1 if partyid_which==subject_cp 
replace pid=0 if partyid_which!=subject_cp 
replace pid=1 if subject_cp== votechoice_list & ccode==22 & year==2012

gen pid_other=1 if partyid_which==other_cp 
replace pid_other=0 if partyid_which!=other_cp 
replace pid_other=1 if other_cp== votechoice_list & ccode==22 & year==2012

gen independent=1 if partyid_which==. 
replace independent=0 if partyid_which!=.  
replace independent=0 if votechoice_list!=. & ccode==22 & year==2012

save temp_all_micro_dyadic.dta, replace



**********************************************************************************************************************************************************************************
* STEP 5: Opening the CCDP dyadic dataset, generating the left-right positions, and mergining the individual-level survey data
**********************************************************************************************************************************************************************************

use CCDP_dyadic.dta, replace

gen top_two=1 if (subject_cmp_code==11620 | subject_cmp_code==11320) & (other_cmp_code==11320 | other_cmp_code==11620) & ccode==11 & year==2010
replace top_two=1 if (subject_cmp_code==11620 | subject_cmp_code==11320) & (other_cmp_code==11320 | other_cmp_code==11620) & ccode==11 & year==2014
replace top_two=1 if (subject_cmp_code==13420 | subject_cmp_code==13320) & (other_cmp_code==13320 | other_cmp_code==13420) & ccode==13 & year==2007
replace top_two=1 if (subject_cmp_code==13420 | subject_cmp_code==13320) & (other_cmp_code==13320 | other_cmp_code==13420) & ccode==13 & year==2011
replace top_two=1 if (subject_cmp_code==22420 | subject_cmp_code==22320) & (other_cmp_code==22420 | other_cmp_code==22320) & ccode==22 & (year==2010 | year==2012)
replace top_two=1 if (subject_cmp_code==33320 | subject_cmp_code==33610) & (other_cmp_code==33610 | other_cmp_code==33320) & ccode==33 & (year==2008 | year==2011)
replace top_two=1 if (subject_cmp_code==35311 | subject_cmp_code==35313) & (other_cmp_code==35311 | other_cmp_code==35313) & ccode==35 & (year==2009 | year==2011)
replace top_two=1 if (subject_cmp_code==41521 | subject_cmp_code==41320) & (other_cmp_code==41521 | other_cmp_code==41320) & ccode==41 & (year==2009 | year==2013)
replace top_two=1 if (subject_cmp_code==51320 | subject_cmp_code==51620) & (other_cmp_code==51320 | other_cmp_code==51620) & ccode==51 & (year==2005 | year==2010 | year==2015)
replace top_two=1 if (subject_cmp_code==82320 | subject_cmp_code==82413) & (other_cmp_code==82320 | other_cmp_code==82413) & ccode==82 & year==2010
replace top_two=1 if (subject_cmp_code==82320 | subject_cmp_code==82430) & (other_cmp_code==82320 | other_cmp_code==82430) & ccode==82 & year==2013
replace top_two=1 if (subject_cmp_code==92435 | subject_cmp_code==92436) & (other_cmp_code==92435 | other_cmp_code==92436) & ccode==92 & (year==2007 | year==2011)

drop if top_two!=1

* generating the party self left-right positions and the positions of each party as discussed by rivals

foreach var of varlist self_issue_1_pos-self_issue_19_neut {
	gen `var'_share= `var'/sum_self_issue
}

foreach var of varlist other_issue_1_pos_received-other_issue_19_neut_received {
	gen `var'_sh= `var'/sum_other_issue_received
}

/* Here is the list of issues:
1- taxes
2- social policy
3- inflation
4- unemplyment
5- other economic policy
6- centralization
7- environment
8- immigration/asylum
9- justice system
10- law and order
11- national way of life
12- traditional morality
13- EU
14- internationalism
15- Foreign intervention
16- agriculture
17-18-19 - country specific (if asked)
*/

gen rile_self=.
replace rile_self= ( self_issue_1_neg_share + ///
			self_issue_3_neg_share + ///
			self_issue_2_neg_share + ///
			self_issue_7_neg_share + ///
			self_issue_8_neg_share + ///
			self_issue_9_pos_share + ///
			self_issue_10_pos_share + ///
 			self_issue_11_pos_share + ///
			self_issue_12_pos_share + ///
			self_issue_15_pos_share) ///
			- ///
			(self_issue_1_pos_share + ///
			self_issue_4_neg_share + ///
			self_issue_2_pos_share + ///
			self_issue_14_pos_share + ///
			self_issue_15_neg_share + ///
			self_issue_7_pos_share + ///
			self_issue_8_pos_share + ///
			self_issue_10_neg_share + ///
			self_issue_11_neg_share + ///
			self_issue_12_neg_share) ///
			
replace rile_self=. if self_issue_1_neg==0 & self_issue_3_neg==0 & self_issue_2_neg==0 & self_issue_7_neg==0 & self_issue_8_neg==0 & self_issue_9_pos==0 &	self_issue_10_pos==0 & self_issue_11_pos==0 & self_issue_12_pos==0 & self_issue_15_pos==0 & self_issue_1_pos==0 & self_issue_4_neg==0 & self_issue_2_pos==0 & self_issue_14_pos==0 & self_issue_15_neg==0 & self_issue_7_pos==0 & self_issue_8_pos==0 & self_issue_10_neg==0 & self_issue_11_neg==0 & self_issue_12_neg==0

gen total_statements_rile_self= self_issue_1_neg + self_issue_3_neg +self_issue_2_neg +self_issue_7_neg + self_issue_8_neg + self_issue_9_pos +	self_issue_10_pos + self_issue_11_pos + self_issue_12_pos + self_issue_15_pos + self_issue_1_pos + self_issue_4_neg + self_issue_2_pos + self_issue_14_pos + self_issue_15_neg + self_issue_7_pos + self_issue_8_pos + self_issue_10_neg + self_issue_11_neg + self_issue_12_neg



gen rile_received=.
replace rile_rec= ( other_issue_1_neg_received_sh + ///
			other_issue_3_neg_received_sh + ///
			other_issue_2_neg_received_sh + ///
			other_issue_7_neg_received_sh + ///
			other_issue_8_neg_received_sh + ///
			other_issue_9_pos_received_sh + ///
			other_issue_10_pos_received_sh + ///
 			other_issue_11_pos_received_sh + ///
			other_issue_12_pos_received_sh + ///
			other_issue_15_pos_received_sh) ///
			- ///
			(other_issue_1_pos_received_sh + ///
			other_issue_4_neg_received_sh + ///
			other_issue_2_pos_received_sh + ///
			other_issue_14_pos_received_sh + ///
			other_issue_15_neg_received_sh + ///
			other_issue_7_pos_received_sh + ///
			other_issue_8_pos_received_sh + ///
			other_issue_10_neg_received_sh + ///
			other_issue_11_neg_received_sh + ///
			other_issue_12_neg_received_sh) ///
			
replace rile_rec=. if other_issue_1_neg_received==0 & other_issue_3_neg_received==0 & other_issue_2_neg_received==0 & other_issue_7_neg_received==0 & other_issue_8_neg_received==0 & other_issue_9_pos_received==0 &	other_issue_10_pos_received==0 & other_issue_11_pos_received==0 & other_issue_12_pos_received==0 & other_issue_15_pos_received==0 & other_issue_1_pos_received==0 & other_issue_4_neg_received==0 & other_issue_2_pos_received==0 & other_issue_14_pos_received==0 & other_issue_15_neg_received==0 & other_issue_7_pos_received==0 & other_issue_8_pos_received==0 & other_issue_10_neg_received==0 & other_issue_11_neg_received==0 & other_issue_12_neg_received==0
	
gen total_statements_rile_rec= other_issue_1_neg_received + other_issue_3_neg_received +other_issue_2_neg_received +other_issue_7_neg_received + other_issue_8_neg_received + other_issue_9_pos_received +	other_issue_10_pos_received + other_issue_11_pos_received + other_issue_12_pos_received + other_issue_15_pos_received + other_issue_1_pos_received + other_issue_4_neg_received + other_issue_2_pos_received + other_issue_14_pos_received + other_issue_15_neg_received + other_issue_7_pos_received + other_issue_8_pos_received + other_issue_10_neg_received + other_issue_11_neg_received + other_issue_12_neg_received  	
	

* merging individual-level data

sort ccode year subject_cp other_cp
	
merge m:m ccode year subject_cp other_cp using temp_all_micro_dyadic.dta


gen index= (ccode*10000)+year

gen pair= (subject_cmp_code*100000)+other_cmp_code

gen selfposition_10= (rile_self+1)*5
gen distorted_pos_10= (rile_rec+1)*5

ren (position_perc_ education_new) (perceived_position education)
	
save observational_final.dta, replace



**********************************************************************************************************************************************************************************
* STEP 6: Testing the models
**********************************************************************************************************************************************************************************

* Table 2: Additive Model Results

xtmixed perceived_position selfposition_10 distorted_pos_10 i.ccode if pid!=. & total_statements_rile_self>1 & total_statements_rile_rec>1, ||index: ||pair:, var
xtmixed perceived_position selfposition_10 distorted_pos_10 i.ccode if pid==1 & total_statements_rile_self>1 & total_statements_rile_rec>1, ||index: ||pair:, var
xtmixed perceived_position selfposition_10 distorted_pos_10 i.ccode if pid_other==1 & total_statements_rile_self>1 & total_statements_rile_rec>1, ||index: ||pair:, var
xtmixed perceived_position selfposition_10 distorted_pos_10 i.ccode if pid==0 & pid_other==0 & independent==0 & total_statements_rile_self>1 & total_statements_rile_rec>1 , ||index: ||pair:, var
xtmixed perceived_position selfposition_10 distorted_pos_10 i.ccode if independent==1 & total_statements_rile_self>1 & total_statements_rile_rec>1 , ||index: ||pair:, var

* Table OA.7.1: The conditional effect of extent of distortion on voter perceptions of party positions, cross-national analysis


gen absdist_rile_10= abs(selfposition_10-distorted_pos_10)
gen absdistxselfposition= selfposition_10*absdist_rile_10


xtmixed perceived_position selfposition_10 absdist_rile_10 absdistxselfposition i.ccode if pid!=. & total_statements_rile_self>1 & total_statements_rile_rec>1, ||index: ||pair:, var
generate MV=((_n-1)/100)
replace  MV=. if _n>271
matrix b=e(b) 
matrix V=e(V)
scalar b1=b[1,1] 
scalar b2=b[1,2]
scalar b3=b[1,3]
scalar varb1=V[1,1]
scalar varb2=V[2,2] 
scalar varb3=V[3,3]
scalar covb1b3=V[1,3] 
scalar covb2b3=V[2,3]
gen conb=b1+b3*MV if _n<272
gen conse=sqrt(varb1+varb3*(MV^2)+2*covb1b3*MV) if _n<272
gen a=1.96*conse
gen upper=conb+a
gen lower=conb-a
graph twoway hist absdist_rile_10, width(0.3) percent color(gs14) yaxis(2) || line conb MV, clwidth(medium) clpattern(solid) clcolor(blue) clcolor(black) yaxis(1) || line upper MV, clpattern(dash) clwidth(thin) clcolor(black) || line lower MV, clpattern(dash) clwidth(thin) clcolor(black)||, xlabel(0 (0.3) 2.7, labsize(2.5)) ylabel(-1 (1) 3,  nogrid labsize(2.5)) ylabel(0 5 10 15 20 25, axis(2) nogrid labsize(2)) yline(0, lcolor(black)) yscale(noline alt) yscale(noline alt axis(2)) xscale(noline) ytitle("Marginal Effect of Self Placement", size(3)) ytitle("Percentages", axis(2) size(2.5)) legend(off) scheme(s2mono) graphregion(fcolor(white) ilcolor(white) lcolor(white)) xtitle("Extent of Distortion", size(3))
graph save allvoters10, replace
drop MV-lower


xtmixed perceived_position selfposition_10 absdist_rile_10 absdistxselfposition i.ccode if pid==1 & total_statements_rile_self>1 & total_statements_rile_rec>1 , ||index: ||pair:, var
generate MV=((_n-1)/100)
replace  MV=. if _n>271
matrix b=e(b) 
matrix V=e(V)
scalar b1=b[1,1] 
scalar b2=b[1,2]
scalar b3=b[1,3]
scalar varb1=V[1,1]
scalar varb2=V[2,2] 
scalar varb3=V[3,3]
scalar covb1b3=V[1,3] 
scalar covb2b3=V[2,3]
gen conb=b1+b3*MV if _n<272
gen conse=sqrt(varb1+varb3*(MV^2)+2*covb1b3*MV) if _n<272
gen a=1.96*conse
gen upper=conb+a
gen lower=conb-a
graph twoway hist absdist_rile_10, width(0.3) percent color(gs14) yaxis(2) || line conb MV, clwidth(medium) clpattern(solid) clcolor(blue) clcolor(black) yaxis(1) || line upper MV, clpattern(dash) clwidth(thin) clcolor(black) || line lower MV, clpattern(dash) clwidth(thin) clcolor(black)||, xlabel(0 (0.3) 2.7, labsize(2.5)) ylabel(-1 (1) 3,  nogrid labsize(2.5)) ylabel(0 5 10 15 20 25, axis(2) nogrid labsize(2)) yline(0, lcolor(black)) yscale(noline alt) yscale(noline alt axis(2)) xscale(noline) ytitle("Marginal Effect Self Placement", size(3)) ytitle("Percentages", axis(2) size(2.5)) legend(off) scheme(s2mono) graphregion(fcolor(white) ilcolor(white) lcolor(white)) xtitle("Extent of Distortion", size(3))
graph save copartisans10, replace
drop MV-lower


xtmixed perceived_position selfposition_10 absdist_rile_10 absdistxselfposition i.ccode if pid_other==1 & total_statements_rile_self>1 & total_statements_rile_rec>1 , ||index: ||pair:, var
generate MV=((_n-1)/100)
replace  MV=. if _n>271
matrix b=e(b) 
matrix V=e(V)
scalar b1=b[1,1] 
scalar b2=b[1,2]
scalar b3=b[1,3]
scalar varb1=V[1,1]
scalar varb2=V[2,2] 
scalar varb3=V[3,3]
scalar covb1b3=V[1,3] 
scalar covb2b3=V[2,3]
gen conb=b1+b3*MV if _n<272
gen conse=sqrt(varb1+varb3*(MV^2)+2*covb1b3*MV) if _n<272
gen a=1.96*conse
gen upper=conb+a
gen lower=conb-a
graph twoway hist absdist_rile_10, width(0.3) percent color(gs14) yaxis(2) || line conb MV, clwidth(medium) clpattern(solid) clcolor(blue) clcolor(black) yaxis(1) || line upper MV, clpattern(dash) clwidth(thin) clcolor(black) || line lower MV, clpattern(dash) clwidth(thin) clcolor(black)||, xlabel(0 (0.3) 2.7, labsize(2.5)) ylabel(-1 (1) 3,  nogrid labsize(2.5)) ylabel(0 5 10 15 20 25, axis(2) nogrid labsize(2)) yline(0, lcolor(black)) yscale(noline alt) yscale(noline alt axis(2)) xscale(noline) ytitle("Marginal Effect Self Placement", size(3)) ytitle("Percentages", axis(2) size(2.5)) legend(off) scheme(s2mono) graphregion(fcolor(white) ilcolor(white) lcolor(white)) xtitle("Extent of Distortion", size(3))
graph save others_part10, replace
drop MV-lower


xtmixed perceived_position selfposition_10 absdist_rile_10 absdistxselfposition i.ccode if pid==0 & pid_other==0 & independent==0 & total_statements_rile_self>1 & total_statements_rile_rec>1 , ||index: ||pair:, var
generate MV=((_n-1)/100)
replace  MV=. if _n>271
matrix b=e(b) 
matrix V=e(V)
scalar b1=b[1,1] 
scalar b2=b[1,2]
scalar b3=b[1,3]
scalar varb1=V[1,1]
scalar varb2=V[2,2] 
scalar varb3=V[3,3]
scalar covb1b3=V[1,3] 
scalar covb2b3=V[2,3]
gen conb=b1+b3*MV if _n<272
gen conse=sqrt(varb1+varb3*(MV^2)+2*covb1b3*MV) if _n<272
gen a=1.96*conse
gen upper=conb+a
gen lower=conb-a
graph twoway hist absdist_rile_10, width(0.3) percent color(gs14) yaxis(2) || line conb MV, clwidth(medium) clpattern(solid) clcolor(blue) clcolor(black) yaxis(1) || line upper MV, clpattern(dash) clwidth(thin) clcolor(black) || line lower MV, clpattern(dash) clwidth(thin) clcolor(black)||, xlabel(0 (0.3) 2.7, labsize(2.5)) ylabel(-1 (1) 3,  nogrid labsize(2.5)) ylabel(0 5 10 15 20 25, axis(2) nogrid labsize(2)) yline(0, lcolor(black)) yscale(noline alt) yscale(noline alt axis(2)) xscale(noline) ytitle("Marginal Effect Self Placement", size(3)) ytitle("Percentages", axis(2) size(2.5)) legend(off) scheme(s2mono) graphregion(fcolor(white) ilcolor(white) lcolor(white)) xtitle("Extent of Distortion", size(3))
graph save thirdparties10, replace
drop MV-lower


xtmixed perceived_position selfposition_10 absdist_rile_10 absdistxselfposition  i.ccode if independent==1 & total_statements_rile_self>1 & total_statements_rile_rec>1, ||index: ||pair:, var
generate MV=((_n-1)/100)
replace  MV=. if _n>271
matrix b=e(b) 
matrix V=e(V)
scalar b1=b[1,1] 
scalar b2=b[1,2]
scalar b3=b[1,3]
scalar varb1=V[1,1]
scalar varb2=V[2,2] 
scalar varb3=V[3,3]
scalar covb1b3=V[1,3] 
scalar covb2b3=V[2,3]
gen conb=b1+b3*MV if _n<272
gen conse=sqrt(varb1+varb3*(MV^2)+2*covb1b3*MV) if _n<272
gen a=1.96*conse
gen upper=conb+a
gen lower=conb-a
graph twoway hist absdist_rile_10, width(0.3) percent color(gs14) yaxis(2) || line conb MV, clwidth(medium) clpattern(solid) clcolor(blue) clcolor(black) yaxis(1) || line upper MV, clpattern(dash) clwidth(thin) clcolor(black) || line lower MV, clpattern(dash) clwidth(thin) clcolor(black)||, xlabel(0 (0.3) 2.7, labsize(2.5)) ylabel(-1 (1) 3,  nogrid labsize(2.5)) ylabel(0 5 10 15 20 25, axis(2) nogrid labsize(2)) yline(0, lcolor(black)) yscale(noline alt) yscale(noline alt axis(2)) xscale(noline) ytitle("Marginal Effect Self Placement", size(3)) ytitle("Percentages", axis(2) size(2.5)) legend(off) scheme(s2mono) graphregion(fcolor(white) ilcolor(white) lcolor(white)) xtitle("Extent of Distortion", size(3))
graph save independents10, replace
drop MV-lower

graph combine allvoters10.gph copartisans10.gph others_part10.gph independents10.gph


* OA.7.2: Robustness Test with Additional Control Variables

gen gender2= gender if gender==1
replace gender2=0 if gender==2

xtmixed perceived_position selfposition_10 distorted_pos_10 age gender2 pol_knowl_share  subject_government_before  i.ccode if pid!=. & total_statements_rile_self>1 & total_statements_rile_rec>1, ||index: ||pair:, var
xtmixed perceived_position selfposition_10 distorted_pos_10 age gender2 pol_knowl_share  subject_government_before  i.ccode if pid==1 & total_statements_rile_self>1 & total_statements_rile_rec>1, ||index: ||pair:, var
xtmixed perceived_position selfposition_10 distorted_pos_10 age gender2 pol_knowl_share  subject_government_before  i.ccode if pid_other==1 & total_statements_rile_self>1 & total_statements_rile_rec>1 , ||index: ||pair:, var
xtmixed perceived_position selfposition_10 distorted_pos_10 age gender2 pol_knowl_share  subject_government_before  i.ccode if pid==0 & pid_other==0 & independent==0 & total_statements_rile_self>1 & total_statements_rile_rec>1, ||index: ||pair:, var
xtmixed perceived_position selfposition_10 distorted_pos_10 age gender2 pol_knowl_share  subject_government_before  i.ccode if independent==1 & total_statements_rile_self>1 & total_statements_rile_rec>1, ||index: ||pair:, var

xtmixed perceived_position selfposition_10 absdist_rile_10 absdistxselfposition age gender2 pol_knowl_share  subject_government_before  i.ccode if pid!=. & total_statements_rile_self>1 & total_statements_rile_rec>1, ||index: ||pair:, var
xtmixed perceived_position selfposition_10 absdist_rile_10 absdistxselfposition age gender2 pol_knowl_share  subject_government_before  i.ccode if pid==1 & total_statements_rile_self>1 & total_statements_rile_rec>1, ||index: ||pair:, var
xtmixed perceived_position selfposition_10 absdist_rile_10 absdistxselfposition age gender2 pol_knowl_share  subject_government_before  i.ccode if pid_other==1 & total_statements_rile_self>1 & total_statements_rile_rec>1 , ||index: ||pair:, var
xtmixed perceived_position selfposition_10 absdist_rile_10 absdistxselfposition age gender2 pol_knowl_share  subject_government_before  i.ccode if pid==0 & pid_other==0 & independent==0 & total_statements_rile_self>1 & total_statements_rile_rec>1, ||index: ||pair:, var
xtmixed perceived_position selfposition_10 absdist_rile_10 absdistxselfposition age gender2 pol_knowl_share  subject_government_before  i.ccode if independent==1 & total_statements_rile_self>1 & total_statements_rile_rec>1, ||index: ||pair:, var
